export * from './auth';
/**
 *  解析微信授权回调参数
 * @author superpositives@163.com
 * @returns  {Object}
 * @date 2021/1/27 17:50:14
 */
export const getWxCallbackUrlParam = function () {
  const obj = {};
  const UrlParam = window.location.search.replace(/\?/g, '');
  const UrlParamArr = UrlParam.split('&');
  for (const key of UrlParamArr) {
    const arr = key.split('=');
    obj[arr[0]] = arr[1];
  }
  return obj;
};

/**
 *  获取域名
 * @author superpositives@163.com
 * @returns  {string}
 * @date 2021/1/27 17:50:41
 */
export const getDomain = () => {
  // const domainArray = document.domain.split(".");
  // eslint-disable-next-line no-irregular-whitespace
  // const arrLength = domainArray.length;
  // let doMainStr = '';
  // for (const [key, index] of new Map(domainArray.map((item, index) => [item, index]))) {
  // eslint-disable-next-line no-irregular-whitespace
  //   if (index === arrLength - 2) {
  // eslint-disable-next-line no-irregular-whitespace
  //     doMainStr += key;
  //   }
  // eslint-disable-next-line no-irregular-whitespace
  //   if (index === arrLength - 1) {
  // eslint-disable-next-line no-irregular-whitespace
  //     doMainStr += '.' + key;
  //   }
  // }
  // return doMainStr;
  const domain = document.domain;
  return domain;
};
/**
 *  设置网页标题
 * @author superpositives@163.com
 * @param { string} title
 * @returns
 * @date 2021/1/28 11:28:25
 */
export const setDocumentTitle = function (title) {
  document.title = title;
  const i = document.createElement('iframe');
  i.style.display = 'none';
  i.onload = function () {
    setTimeout(function () {
      i.remove();
    }, 9);
  };
  document.body.appendChild(i);
  return true;
};
/**
 *  跳转授权链接得到code
 * @author superpositives@163.com
 * @param {Object}
 * @date 2021/1/28 10:42:14
 */
export const goToGetCode = ({ appid, realm, state }) => {
  const conifigObj = {
    appid: appid,
    redirectUrl: encodeURIComponent(realm),
    // 'snsapi_base' -- 不弹出授权页面，直接跳转，只能获取用户openid 'snsapi_userinfo' --- 弹出授权页面，可通过openid拿到昵称、性别、所在地
    scope: 'snsapi_base',
    state: state
  };
  window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${conifigObj.appid}&redirect_uri=${conifigObj.redirectUrl}&response_type=code&scope=${conifigObj.scope}&state=${conifigObj.state}#wechat_redirect`;
};
/**
 * @Description: 跳转授权链接得到code scope为snsapi_userinfo
 * @Param:
 * @return:
 * @Author: jjc
 * @Date: 2021/2/25
 */
export const goToGetCodeUserinfo = ({ appid, realm, state }) => {
  const conifigObj = {
    appid: appid,
    redirectUrl: encodeURIComponent(realm),
    // 'snsapi_base' -- 不弹出授权页面，直接跳转，只能获取用户openid 'snsapi_userinfo' --- 弹出授权页面，可通过openid拿到昵称、性别、所在地
    scope: 'snsapi_userinfo',
    state: state
  };
  console.log(conifigObj);
  window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${conifigObj.appid}&redirect_uri=${conifigObj.redirectUrl}&response_type=code&scope=${conifigObj.scope}&state=${conifigObj.state}#wechat_redirect`;
};
/**
 *  拼接路径
 * @author superpositives@163.com
 * @param {string} path
 * @returns {string}
 * @date 2021/1/28 14:20:21
 */
export const getPathUrl = (path) => {
  const { origin } = window.location;
  return `${origin + '/html/#' + path}`;
};
/**
 *  判断设备
 * @author superpositives@163.com
 * @returns  {string}
 * @date 2021/2/2 10:51:22
 */
export const isAndroid = () => {
  const u = navigator.userAgent;
  const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; // android终端
  return isAndroid ? 'Android' : 'IOS';
};

/**
 *  抖音授权回调参数解析
 * @author superpositives@163.com
 * @param { object } val
 * @returns  { object}
 * @date 2021/2/5 14:45:39
 */
export const getDyCallbackUrlParam = (val) => {
  const params = val;
  const stateObj = {
    code: params.code || null
  };
  const strAtob = window.atob(decodeURIComponent(params.state));
  console.log(strAtob);
  for (const key of strAtob.split('&')) {
    const keyArr = key.split('=');
    stateObj[keyArr[0]] = Number(keyArr[1]);
  }
  return stateObj;
};

